<template>
  <div class="login_container">
    <div class="login_box">
      <!-- logo区域 -->
      <el-row class="login_layout">
        <el-col :span="24">
          <div class="grid-content">
            <img class="img_box" src="../assets/img/quartz.jpg" alt="" />
          </div>
        </el-col>
      </el-row>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFromRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
     name:'Login',
  components: {},
  data() {
    return {
      // 这是登录表单的数据绑定对象
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "用户名在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "密码在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 点击重置按钮，重置登录表单
    resetLoginForm() {
      this.$refs.loginFromRef.resetFields();
    },
    // 登录按钮
    login() {
      // 表单登录预验证
      this.$refs.loginFromRef.validate( (valid) => {
        if (!valid) return
        // const { data: res } = await this.$http.post('login', this.loginForm)
        // if (res.meta.status !== 200) return this.$message.error('登录失败 ！')
        if(this.loginForm.username=='admin'&& this.loginForm.password == '123456')
        {
            this.$message.success('登录成功 ！');
            // 1.2 token 只应在当前网站打开期间生效，所以将token保存在sessionStorage中
            window.sessionStorage.setItem('token', "admin");
            this.$router.push('/home');
        }
        else
        {
          this.$message.error('登录失败 ！');
        }
        
        // 
        // localStorage(持久化的)
        // 1,将登录成功后的token 保存到客户端的 sessionStorage（只存在会话期间）中
        // 1.1项目中除了登录之外的Api接口，必须在登录后才能访问
        // 
        // 
        // 2，通过编程式导航跳转到后台页面，路由地址是Home
        // this.$router.push('/home')
      })
    }

  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-image: url(../assets/img/loginbg.jpg);
  background-size: cover;
  background-attachment: fixed;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 20px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.login_layout {
  border-radius: 20px;
  background: #fff;
  overflow: hidden;
}
.bg-left {
  background: #fff;
}
.bg-right {
  background: #fff;
}
.grid-content {
  min-height: 15px;
  text-align: center;
}
.img_box {
  height: 50%;
  width: 50%;
  margin-top: 15px;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>